//
//  CustomerServiceViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/25.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "CustomerServiceViewController.h"
#import <RongIMLib/RongIMLib.h>
#import "RCDCustomerServiceViewController.h"
#import "CustomerServiceCell.h"

NSString * const CustomerServiceCellIdentifier = @"CustomerServiceCellIdentifier";
@interface CustomerServiceViewController ()
@property(nonatomic, copy) NSString * phoneNo;
@end

@implementation CustomerServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle:@"联系客服"];
    [self requestPhone];
//    [self connectionServer];
    // 设置消息接收监听
    [[RCIMClient sharedRCIMClient] setReceiveMessageDelegate:self object:nil];
    int totalUnreadCount = [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    NSLog(@"当前所有会话的未读消息数为：%d", totalUnreadCount);
 
}

- (void)requestPhone{
    
    
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Common/getKfrx" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            strongSelf.phoneNo = responseObject[@"data"][@"info"];
            
        }
        [self.tableView reloadData];
    }];
}
- (void)registerCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomerServiceCell class]) bundle:nil] forCellReuseIdentifier:CustomerServiceCellIdentifier];
}
- (void)gotoChatService{
    
    RCDCustomerServiceViewController *chatService = [[RCDCustomerServiceViewController alloc] init];
#define SERVICE_ID @"KEFU152704676339152" //您在融云后台开通的客服ID
//    chatService.userName = chatService.userName;
    chatService.conversationType = ConversationType_CUSTOMERSERVICE;
    chatService.targetId = SERVICE_ID;
//    chatService.title =  @"客服";
    [self.navigationController pushViewController :chatService animated:YES];
}


- (void)connectionServer{
    
    [[RCIMClient sharedRCIMClient] connectWithToken:@"YourTestUserToken"
                                            success:^(NSString *userId) {
                                                NSLog(@"登陆成功。当前登录的用户ID：%@", userId);
                                            } error:^(RCConnectErrorCode status) {
                                                NSLog(@"登陆的错误码为:%d", status);
                                            } tokenIncorrect:^{
                                                //token过期或者不正确。
                                                //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
                                                //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
                                                NSLog(@"token错误");
                                            }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    CustomerServiceCell * tempCell = [tableView dequeueReusableCellWithIdentifier:CustomerServiceCellIdentifier];
    NSString * titleStr = @"";
    if (indexPath.row == 0) {
        titleStr = @"在线客服（8：00-18：00）";
    }else{
        titleStr = [NSString stringWithFormat:@"人工客服（8：00-18：00）\n%@",self.phoneNo];
    }
    [tempCell setupTitle:titleStr];
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        [self gotoChatService];
    }else if (indexPath.row == 1){
        
        [self gotoPhone];
    }
}

- (void)gotoPhone{
    NSString * callPhone = [NSString stringWithFormat:@"telprompt://%@",self.phoneNo];
    
    if (@available(iOS 10.0, *)) {
        /// 大于等于10.0系统使用此openURL方法
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
